package com.dq.domain.sys.user.user;

import com.dq.domain.service.ServiceProject;
import com.dq.domain.sys.role.SysRole;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.vo.baseVo.VO;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.user.user.base.BackUser;
import com.dq.utils.TimeUtil;
import com.google.common.base.Joiner;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author liuhui
 * @date 2019/12/3 0003 10:39:26
 */
@Data
@Entity
@ApiModel("boss用户")
public class BossUser extends BackUser implements VO.ToVO {
    @ApiModelProperty("上班时间")
    protected String startTime;
    @ApiModelProperty("下班时间")
    protected String endTime;
    @ApiModelProperty("评价次数")
    @Column(columnDefinition="bigint default 0")
    protected Long num;
    @ApiModelProperty("平均分")
    @Column(columnDefinition="decimal default 0")
    protected BigDecimal averageScore;

    @ApiModelProperty("员工状态")
    @Enumerated(EnumType.ORDINAL)
    protected StaffUserStatus staffUserStatus;

    @ApiModelProperty("员工离职时间")
    protected Long leaveTime;

    public enum StaffUserStatus{
        on("在职"),
        off("离职");

        public String name;

        StaffUserStatus(String name){
            this.name = name;
        }
    }

    public BossUser() {
    }
    public BossUser(String username, String localNickName, String userRealcName, String password, Status status, String cardNo, SysSection section) {
        this.username = username;
        this.localNickName = localNickName;
        this.userRealcName = userRealcName;
        this.password = password;
        this.status = status;
        this.cardNo = cardNo;
        this.section = section;
    }

    @Data
    @ApiModel("boss用户详情展示")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty(value = "用户编号")
        private String cardNo;
        @ApiModelProperty("真实姓名")
        protected String userRealcName;
        @ApiModelProperty("账号")
        protected String username;
        @ApiModelProperty(value = "头像")
        private String headImg;
        @ApiModelProperty("充值提成比例")
        protected BigDecimal topRatio;
        @ApiModelProperty("消耗提成比例")
        protected BigDecimal expendRatio;
        @ApiModelProperty(value = "手机号")
        private String mobile;
        @ApiModelProperty(value = "组织部门/门店")
        private Long sectionId;
        @ApiModelProperty(value = "组织部门/门店")
        private String sectionSimpleName;
        @ApiModelProperty(value = "性别")
        protected String genderEn;
        protected String genderCn;

        @ApiModelProperty("省份")
        protected String province;
        @ApiModelProperty("城市")
        protected String city;
        @ApiModelProperty("区")
        protected String district;

        @ApiModelProperty("状态")
        protected String statusEn;
        @ApiModelProperty("状态")
        protected String statusCn;

        @ApiModelProperty("身份证号码")
        protected String idCardNo;
        @ApiModelProperty("入职时间/入会时间")
        protected Long entryTime;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "更新时间")
        private String updateTime;
        @ApiModelProperty(value = "创建人")
        private String createUsername;
        @ApiModelProperty(value = "修改人")
        private String updateUsername;

        /** todo 需赋值 */
        @ApiModelProperty(value = "员工职位")
        private String roleShowName;
        @ApiModelProperty(value = "角色code列表")
        private Set<String> roleCodes;
        @ApiModelProperty("菜单id列表")
        private List<SysMenu.Vo> menuList;
        @ApiModelProperty("按钮id列表")
        private Set<SysMenu.Vo> buttonList;
        /** 赋值 */
        @ApiModelProperty("角色集合")
        private Set<SysRole.Vo> roleList = new HashSet<>();
        /** 赋值 */
        @ApiModelProperty("关联服务项目")
        private Set<ServiceProject.Vo> serviceProjectList = new HashSet<>();

        @ApiModelProperty("token")
        private String token;

        protected String postIds;
        protected String roleIds;
        protected String roleNames;

        @ApiModelProperty("角色id")
        private List<Long> roleId;
        @ApiModelProperty("角色名")
        private List<String> roleName;
        @ApiModelProperty("角色code")
        private List<String> roleCode;
        @ApiModelProperty("上班时间")
        private String startTime;
        @ApiModelProperty("下班时间")
        private String endTime;
        @ApiModelProperty("员工状态")
        private String staffUserStatusEn;
        private String staffUserStatusCn;
        @ApiModelProperty("员工离职时间")
        protected String leaveTime;
        @ApiModelProperty("总提成")
        protected BigDecimal sumCommission;
    }

    @Data
    @ApiModel(value = "员工用户视图")
    public static class LoginVo {
        private Long id;
        @ApiModelProperty(value = "用户编号")
        private String cardNo;
        @ApiModelProperty(value = "用户登陆名")
        private String username;
        @ApiModelProperty("用户真实姓名")
        private String userRealcName;
        @ApiModelProperty(value = "邮箱")
        private String email;
        @ApiModelProperty(value = "手机号")
        private String mobile;
        @ApiModelProperty("充值提成比例")
        protected BigDecimal topRatio;
        @ApiModelProperty("消耗提成比例")
        protected BigDecimal expendRatio;
        @ApiModelProperty("状态")
        protected String statusEn;
        @ApiModelProperty("状态")
        protected String statusCn;
        @ApiModelProperty(value = "性别")
        protected String genderEn;
        protected String genderCn;

        @ApiModelProperty("身份证号码")
        protected String idCardNo;
        @ApiModelProperty("入职时间/入会时间")
        protected Long entryTime;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "修改时间")
        private String updateTime;
        @ApiModelProperty(value = "创建人")
        private String createUsername;
        @ApiModelProperty(value = "修改人")
        private String updateUsername;
        @ApiModelProperty(value = "头像")
        private String headImg;
        @ApiModelProperty(value = "邀请人微信手机号")
        private String inviteUserMobile;
        @ApiModelProperty("token")
        private String token = "123456";
        @ApiModelProperty("绑定门店")
        private Long orgId;
        private String localNickName;

        @ApiModelProperty(value = "组织部门/门店")
        private Long sectionId;
        @ApiModelProperty(value = "组织部门/门店")
        private String sectionSimpleName;

        @ApiModelProperty("角色id")
        private List<Long> roleId;
        @ApiModelProperty("角色名")
        private List<String> roleName;
        @ApiModelProperty("角色code")
        private List<String> roleCode;

        @ApiModelProperty("上班时间")
        protected String startTime;
        @ApiModelProperty("下班时间")
        protected String endTime;
        @ApiModelProperty("目标业绩")
        protected BigDecimal target;
        protected BigDecimal averageScore;
        @ApiModelProperty("标签(数目)")
        protected List<String> lableStrList;

    }

    public LoginVo toLoginVo() {
        BossUser.LoginVo loginVo = new BossUser.LoginVo();
        loginVo.id = this.id;
        loginVo.cardNo = this.cardNo;
        loginVo.username = this.username;
        loginVo.userRealcName = this.getUserRealcName();
        loginVo.email = this.email;
        loginVo.mobile = this.mobile;
        loginVo.topRatio = this.getTopRatio();
        loginVo.expendRatio = this.getExpendRatio();
        loginVo.statusEn = this.getStatus() == null ? null : this.getStatus().toString();
        loginVo.statusCn = this.getStatus() == null ? null : this.getStatus().name;
        loginVo.genderEn = this.getGender() == null ? Gender.xx.toString() : this.getGender().toString();
        loginVo.genderCn = this.getGender() == null ? Gender.xx.name : this.getGender().name;

        loginVo.idCardNo = fmt(this.getIdCardNo());
        loginVo.entryTime = fmt(this.getEntryTime());

        loginVo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        loginVo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        loginVo.createUsername = this.getCreateUser() == null ? "" : this.getCreateUser().getUserRealcName();
        loginVo.updateUsername = this.getUpdateUser() == null ? "" : this.getUpdateUser().getUserRealcName();
        loginVo.orgId = this.getSection().getId();
        loginVo.localNickName = this.getLocalNickName();
        loginVo.headImg = this.getHeadImg();
        loginVo.startTime = this.getStartTime();
        loginVo.endTime = this.getEndTime();
        loginVo.averageScore = this.getAverageScore();

        loginVo.sectionId = this.getSection() == null ? null : this.getSection().getId();
        loginVo.sectionSimpleName = this.getSection() == null ? "" : this.getSection().getSimpleName();

        return loginVo;
    }


    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.getId();
        vo.cardNo = fmt(this.cardNo);
        vo.userRealcName = fmt(this.getUserRealcName());
        vo.username = fmt(this.getUsername());
        vo.headImg = fmt(this.headImg);
        vo.topRatio = this.getTopRatio();
        vo.expendRatio = this.getExpendRatio();
        vo.statusEn = this.getStatus() == null ? null : this.getStatus().toString();
        vo.statusCn = this.getStatus() == null ? null : this.getStatus().name;
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createUsername = this.getCreateUser() == null ? "" : this.getCreateUser().getUserRealcName();
        vo.updateUsername = this.getUpdateUser() == null ? "" : this.getUpdateUser().getUserRealcName();

        vo.genderEn = this.getGender() == null ? Gender.xx.toString() : this.getGender().toString();
        vo.genderCn = this.getGender() == null ? Gender.xx.name : this.getGender().name;

        vo.idCardNo = fmt(this.getIdCardNo());
        vo.entryTime = this.getEntryTime();

        vo.sectionId = this.getSection() == null ? null : this.getSection().getId();
        vo.sectionSimpleName = this.getSection() == null ? "" : this.getSection().getSimpleName();

        vo.province = fmt(this.getProvince());
        vo.city = fmt(this.getCity());
        vo.district = fmt(this.getDistrict());
        vo.mobile = fmt(this.mobile);

        List<Long> longs = new ArrayList<>();
        OrgPosition orgPosition = this.getOrgPosition();
        Long postId = null;
        if(orgPosition != null){
            postId = orgPosition.getId();
            longs.add(postId);
        }
        vo.postIds = orgPosition!= null? Joiner.on(",").join(longs):"";
        vo.roleIds = this.roleIds;
        vo.roleNames = this.roleNames;
        vo.startTime = this.getStartTime();
        vo.endTime = this.getEndTime();
        vo.staffUserStatusCn =this.getStaffUserStatus() != null ? this.getStaffUserStatus().name : "";
        vo.staffUserStatusEn = this.getStaffUserStatus() != null ? this.getStaffUserStatus().toString() : "";
        vo.leaveTime = fmt(this.getLeaveTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        return vo;
    }

    @Data
    public static class FrontBossUser{
        @ApiModelProperty("平均分")
        protected BigDecimal averageScore;
        protected Long id;
        @ApiModelProperty("真实姓名")
        protected String userRealcName;
        protected String orgName;
    }

    public FrontBossUser toFrontBossUser(){
        FrontBossUser vo  = new FrontBossUser();
        vo.averageScore = this.getAverageScore();
        vo.id = this.getId();
        vo.userRealcName = this.getUserRealcName();
        vo.orgName = this.getSection() != null ? this.getSection().getSimpleName():"";
        return vo;
    }

}
